package com.example.myproject;

import com.example.myproject.Entity.LoginInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;

import java.util.List;

@Controller
public class LoginLogController {
    @Autowired
    private LoginInfoRepository loginInfoRepository;
    @Autowired
    private LoginService loginService;

    @PostMapping("/login")
    public ResponseEntity<String> login(@RequestParam("username") String username) {
        // 调用 LoginService 的 login 方法进行用户认证
        boolean isAuthenticated = loginService.login(username);

        if (isAuthenticated) {
            return ResponseEntity.ok("登录成功");
        } else {
            return ResponseEntity.status(HttpStatus.UNAUTHORIZED).body("用户名或密码错误");
        }
    }
    @GetMapping("/login")
    public String showLoginLogs(Model model) {
        List<LoginInfo> loginInfos = loginInfoRepository.findAll();
        model.addAttribute("loginInfos", loginInfos);
        return "login";
    }
}
